package cn.kgm.user.server.controller;

import cn.kgm.core.http.request.BaseController;
import cn.kgm.core.http.response.R;
import cn.kgm.user.dto.dto.UserLoginLogSaveDTO;
import cn.kgm.user.dto.dto.UserLoginLogUpdateDTO;
import cn.kgm.user.dto.entity.UserLoginLog;
import cn.kgm.user.dto.query.UserLoginLogPageQuery;
import cn.kgm.user.dto.vo.UserLoginLogVO;
import cn.kgm.user.server.service.UserLoginLogService;
import cn.kgm.user.server.struct.UserLoginLogMappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * # 技术改变自己 #
 * 用户登录日志表
 * @author kappi
 * @date   2024-04-08 13:28:48
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/userLoginLog")
@Api(value = "UserLoginLog", tags = "用户登录日志表")
public class UserLoginLogController extends BaseController {

    @Autowired
    private UserLoginLogService userLoginLogService;

    /**
     * 分页列表查询用户登录日志表
     */
    @ApiOperation(value = "分页查询", notes = "分页查询用户登录日志表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "long", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页条数", dataType = "long", paramType = "query", defaultValue = "20"),
    })
    @GetMapping(value = "/page")
    public R<IPage<UserLoginLogVO>> page(UserLoginLogPageQuery data) {
        IPage<UserLoginLog> page = getPage();
        // 构建不为null的查询条件
        UserLoginLog userLoginLog =UserLoginLogMappers.INSTANCE.mapToEntity(data);
        LambdaQueryWrapper<UserLoginLog> wrapper = Wrappers.lambdaQuery(userLoginLog);
        // 分页查询
            userLoginLogService.page(page, wrapper);
        return R.success(page.convert((UserLoginLogMappers.INSTANCE::mapToEntity)));
    }

    /**
     * 根据id查询用户登录日志表详情
     */
    @ApiOperation(value = "查看详情", notes = "根据id查询用户登录日志表详情")
    @GetMapping(value = "/{id}")
    public R<UserLoginLogVO> get(@PathVariable(value = "id") Long id) {
        UserLoginLog userLoginLog =userLoginLogService.getById(id);
        return R.success(UserLoginLogMappers.INSTANCE.mapToEntity(userLoginLog));
    }

    /**
     * 新增用户登录日志表
     */
    @ApiOperation(value = "新增", notes = "新增用户登录日志表")
    @PostMapping("/save")
    public R<Boolean> save(@RequestBody @Validated UserLoginLogSaveDTO data) {
        UserLoginLog userLoginLog =UserLoginLogMappers.INSTANCE.mapToEntity(data);
            userLoginLogService.save(userLoginLog);
        return R.success();
    }

    /**
     * 修改用户登录日志表
     */
    @ApiOperation(value = "修改", notes = "修改用户登录日志表")
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody @Validated UserLoginLogUpdateDTO data) {
        UserLoginLog userLoginLog =UserLoginLogMappers.INSTANCE.mapToEntity(data);
        userLoginLogService.updateById(userLoginLog);
        return R.success();
    }

    /**
     * 根据id更新用户登录日志表的删除状态
     */
    @ApiOperation(value = "删除", notes = "根据id更新用户登录日志表的删除状态")
    @GetMapping(value = "/delete/{ids}")
    public R<Boolean> delete(@PathVariable(value = "ids") Long[] ids) {
            userLoginLogService.update(Wrappers.<UserLoginLog>lambdaUpdate()
                .set(UserLoginLog::getIsDel, true)
                .in(UserLoginLog::getId, ids));
        return R.success();
    }
}
